#include "mybutton.h"
#include "playscene.h"
#include "mycoin.h"
#include "dataconfig.h"
#include <QDebug>
#include <QLabel>
#include <QMenuBar>
#include <QPainter>
#include <QTimer>
#include <QPropertyAnimation>
#include <QSound>

PlayScene::PlayScene(int levelNum){

    this->levelIndex = levelNum;

//    QString str = QString("您选择了第 %1 关").arg(levelIndex);
//    qDebug()<<str;

    //设置固定大小
    setFixedSize(320,588);
    //设置图标
    setWindowIcon(QIcon(":/Coin0001.png"));
    //设置标题
    setWindowTitle("翻金币场景");
    //添加菜单栏
    QMenuBar * bar = menuBar();
    setMenuBar(bar);
    QMenu * startMenu = bar->addMenu("开始");
    QAction * quit = startMenu->addAction("退出");
    connect(quit,&QAction::triggered,this,&PlayScene::close);

    //返回按钮
    MyButton * backBtn = new MyButton(":/BackButton.png",":/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width() - backBtn->width(),this->height() - backBtn->height());

    QSound * backSound = new QSound(":/BackButtonSound.wav");
    QSound * coinFlipSound = new QSound(":/ConFlipSound.wav");
    QSound * winSound = new QSound(":/LevelWinSound.wav");

    connect(backBtn,&MyButton::clicked,[=](){
        //qDebug()<<"返回";

        backSound->play();

        QTimer::singleShot(500,[=](){
            emit this->chooseSceneBack();
        });
    });

    //显示当前关卡数
    QLabel * label = new QLabel;
    label->setParent(this);
    label->setGeometry(30,this->height() - 50,120,50);
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    QString strFont = QString("Level: %1").arg(levelIndex);
    label->setFont(font);
    label->setText(strFont);

    dataConfig config;
    //初始化每个关卡的二维数组
    for(int i=0; i<4; i++){
        for(int j=0; j<4; j++){
            this->gameArray[i][j] = config.mData[this->levelIndex][i][j];
        }
    }

    //显示胜利图片
    QLabel * winLabel = new QLabel;
    QPixmap tmpPix;
    tmpPix.load(":/LevelCompletedDialogBg.png");
    winLabel->setGeometry(0,0,tmpPix.width(),tmpPix.height());
    winLabel->setPixmap(tmpPix);
    winLabel->setParent(this);
    winLabel->move((this->width() - tmpPix.width())*0.5,-tmpPix.height());

    //显示金币背景图
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < 4; j++){
            QLabel * label = new QLabel;
            label->setParent(this);
            label->setGeometry(0,0,50,50);
            label->move(57+i*50,200+j*50);
            label->setPixmap(QPixmap(":/BoardNode.png"));

            //创建金币
            QString str;
            if(this->gameArray[i][j]==1){
                str = ":/Coin0001.png";
            }else{
                str = ":/Coin0008.png";
            }
            MyCoin * coin = new MyCoin(str);
            coin->setParent(this);
            coin->move(59+i*50,204+j*50);

            //给金币属性赋值
            coin->posX = i;
            coin->posY = j;
            coin->flag = this->gameArray[i][j];

            coinBtn[i][j] = coin;

            //点击金币反转
            connect(coin,&MyCoin::clicked,[=](){

                coinFlipSound->play();

                coin->changeFlag();
                this->gameArray[i][j] = this->gameArray[i][j] == 0 ? 1 : 0;

                QTimer::singleShot(300,this,[=](){
                    if(coin->posX + 1<=3){
                        coinBtn[coin->posX+1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX+1][coin->posY] = this->gameArray[coin->posX+1][coin->posY] == 0 ? 1 : 0;
                    }
                    if(coin->posX - 1>=0){
                        coinBtn[coin->posX-1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX-1][coin->posY] = this->gameArray[coin->posX-1][coin->posY] == 0 ? 1 : 0;
                    }
                    if(coin->posY + 1<=3){
                        coinBtn[coin->posX][coin->posY+1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY+1] = this->gameArray[coin->posX][coin->posY+1] == 0 ? 1 : 0;
                    }
                    if(coin->posY - 1>=0){
                        coinBtn[coin->posX][coin->posY-1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY-1] = this->gameArray[coin->posX][coin->posY-1] == 0 ? 1 : 0;
                    }

                    this->isWin = true;

                    for(int i = 0;i < 4; i++){
                        for(int j = 0;j < 4; j++){
                            if(coinBtn[i][j]->flag == false){
                                this->isWin = false;
                                break;
                            }
                        }
                    }

                    if(this->isWin == true){
//                        qDebug()<<"Win";

                        winSound->play();

                        for(int i = 0;i < 4; i++){
                            for(int j = 0;j < 4; j++){
                                coinBtn[i][j]->isWin = true;
                            }
                        }

                        //将胜利图片显示出来
                        QPropertyAnimation * animation = new QPropertyAnimation(winLabel,"geometry");
                        //设置动画时间间隔
                        animation->setDuration(1000);
                        //设置动画起始位置
                        animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                        //设置动画结束位置
                        animation->setEndValue(QRect(winLabel->x(),winLabel->y()+114,winLabel->width(),winLabel->height()));
                        //设置动画弹跳曲线
                        animation->setEasingCurve(QEasingCurve::OutBounce);
                        //执行动画
                        animation->start();
                    }
                });

            });

        }
    }
}

void PlayScene::paintEvent(QPaintEvent *){
    QPainter painter(this);
    QPixmap pix;

    //加载背景
    pix.load(":/PlayLevelSceneBg.png");
    //绘制背景
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    //加载图标
    pix.load(":/Title.png");
    //图标缩放
    pix = pix.scaled(pix.width() * 0.5,pix.height() * 0.5);
    //绘制图标
    painter.drawPixmap(10,30,pix);
}
